package com.gohome.gohomechat.model;

import android.os.Handler;
import android.os.Message;
import android.os.NetworkOnMainThreadException;
import android.util.Log;

import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.gohome.gohomechat.GoHomeModel;
import com.gohome.gohomechat.model.entify.Friend;
import com.gohome.gohomechat.model.entify.Group;
import com.gohome.gohomechat.model.modellistener.AddressModelInterface;
import com.gohome.gohomechat.model.modellistener.GroupChatModelInterface;
import com.gohome.gohomechat.presenter.callback.AddressCallBack;
import com.gohome.gohomechat.presenter.callback.GroupChatCallBack;
import com.gohome.gohomechat.utils.ToastUtil;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by dllo on 16/3/1.
 */
public class GroupChatModel implements GroupChatModelInterface {
    private Handler handler;
    private List<Group> emGroups = new ArrayList<Group>();
    List<String> groupIds = new ArrayList<String>();
    private List<EMGroup> grouplist;

    @Override
    public void refreshData(final GroupChatCallBack callBack) {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                callBack.onsuccess((List<Group>) msg.obj);
                return false;
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    emGroups.clear();
                    groupIds.clear();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    grouplist = EMGroupManager.getInstance().getGroupsFromServer();//需异步处理
                    Log.i("wcl", "获取群组列表" + grouplist.size());
                    for (EMGroup emGroup : grouplist) {
                        emGroups.add(new Group(emGroup.getGroupName(), emGroup.getGroupId()));
                        // groupIds.add(emGroup.getGroupId());

                    }
                    Collections.sort(emGroups); // 升序排列
                    Message message = new Message();
                    message.what = 200;
                    message.obj = emGroups;
                    handler.sendMessage(message);
//                    ParseQuery<ParseObject> parseQuery = new ParseQuery("AllGroupGoHome");
//                    parseQuery.whereContainedIn("groupid", groupIds);
//                    parseQuery.findInBackground(new FindCallback<ParseObject>() {
//                        @Override
//                        public void done(List<ParseObject> objects, ParseException e) {
//                            if (e == null) {
//                                Log.i("wcl", "获取群组数据--");
//                                for (int i = 0; i < objects.size(); i++) {
//                                    ParseFile parseFile = objects.get(i).getParseFile("grouphead_img");
//                                    emGroups.add(new Group(grouplist.get(i).getGroupName(), groupIds.get(i), parseFile.getUrl()));
//                                    Log.i("wcl", parseFile.getUrl());
//                                }
//                                Collections.sort(emGroups); // 升序排列
//                                callBack.onsuccess(emGroups);
//                            } else {
//                                callBack.failed();
//                                Log.i("wcl", e.getMessage() + "  lll");
//                            }
//                        }
//                    });
                } catch (EaseMobException e) {
                    Log.i("wcl", "获取群组列表失败" + e.getMessage());
                    callBack.failed();
                }
            }
        }).start();
    }
}
